package com.hqq.exercise.array;

/**
 * ContinuesSequence 和为s的连续正数序列
 * 题目描述:
 * 输入一个正数s，打印出所有和为s的连续正数序列（至少含两个数）
 * 样例输入:
 * 15
 * 样例输出:
 * 1 2 3 4 5
 * 4 5 6
 * 7 8
 * 思路:
 * Created by heqianqian on 2017/8/23.
 */
public class ContinuesSequence {

    public static void main(String[] args) {
        System.out.println("Summary 9: ");
        getContinueSequence(9);
        System.out.println("Summary 15: ");
        getContinueSequence(15);
    }

    public static void getContinueSequence(int num) {
        if (num < 0) {
            return;
        }
        int small = 1;
        int big = 2;
        int sum = small + big;
        while (small < ((num + 1) / 2)) {//当两个大于一个数一半的数相加的结果一定大于该数 所以出口条件的small=(num+1)/2
            if (sum == num) {
                System.out.println(small + "-" + big);
                sum -= small;
                small++;
                big++;
                sum += big;
            }
            if ((small < big) && (sum < num)) {
                big++;
                sum += big;
            }
            if ((small < big) && (sum > num)) {
                sum -= small;
                small++;
            }
        }
    }


}
